/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.activity.helper;

import org.eclipse.uml2.uml.Action;
import org.eclipse.uml2.uml.Activity;
import org.eclipse.uml2.uml.ActivityNode;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ObjectFlow;
import org.eclipse.uml2.uml.ObjectNode;
import org.eclipse.uml2.uml.Pin;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * Manages an ObjectFlow
 *
 * @author Gilles Cannenterre
 * @since 23 mars 07
 */
public class EdgeObjectFlowHelper extends EdgeActivityEdgeHelper {
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#canCreateUMLElement(org.eclipse.uml2.uml.Element)
	 */
	@Override
	public boolean canCreateUMLElement(Element umlParent) {
		if (umlParent instanceof Activity) {
			return true;
		}
		return false;
	}

	public boolean canConnect(Element source, Element target) {
		if(target == null) {
			if (source instanceof ObjectNode){return true;}
		} else {
			if (source instanceof ObjectNode && target instanceof ObjectNode){return true;}
		}
		return false;
	}
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#getBaseString()
	 */
	@Override
	public String getBaseString() {
		return "ObjectFlow_"; //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#createUMLElement(java.lang.String, org.eclipse.uml2.uml.Element)
	 */
	@Override
	public Element createUMLElement(String name, Element umlParent){
		ObjectFlow objectFlow;
		if (umlParent instanceof Pin) {
			objectFlow = (ObjectFlow) ((Action)((Pin)umlParent).getOwner()).getActivity().createEdge(name, UMLPackage.eINSTANCE.getObjectFlow());		
			objectFlow.createGuard("Guard", null, UMLPackage.eINSTANCE.getLiteralString());
			objectFlow.createWeight("Weight", null, UMLPackage.eINSTANCE.getLiteralString());
			return objectFlow;	
		} 
		if (umlParent instanceof ActivityNode) {
			objectFlow = (ObjectFlow) ((ActivityNode)umlParent).getActivity().createEdge(name, UMLPackage.eINSTANCE.getObjectFlow());
			objectFlow.createGuard("Guard", null, UMLPackage.eINSTANCE.getLiteralString());
			objectFlow.createWeight("Weight", null, UMLPackage.eINSTANCE.getLiteralString());
			return objectFlow;	
		}	
		return null;
	}	
}
